#include "mainwindow.h"
#include <QFile>
#include <QApplication>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QDebug>
#include <QWindow>
#include <QTextCodec>
#include "src/common/singleapplication.h"
#include "src/common/screenmanager.h"
#include "src/common/xwindowmanager.h"
#include "src/subwindow.h"
#include "stdlib.h"

void set_system_ThemeLanguage()
{
    char* language = nullptr;
    char* platform = nullptr;

    language = getenv("LANG");
    if (language == nullptr)
        putenv((char*)"LANG=zh_CN.UTF-8");

    platform = getenv("QT_QPA_PLATFORMTHEME");
    if (platform == nullptr)
        putenv((char*)"QT_QPA_PLATFORMTHEME=ukui");
}
int main(int argc, char *argv[])
{
//    qputenv("QT_IM_MODULE", QByteArray("qtvirtualkeyboard"));
    set_system_ThemeLanguage();
    SingleApplication a(argc, argv, false, SingleApplication::SecondaryNotification);
//    QApplication a(argc, argv);
    MainWindow w;
	
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForLocale(codec);

    ScreenManager screenMgr;
    screenMgr.setPrimaryScreenWidget(&w);
    QList<QScreen*> listScreen = qApp->screens();
    qDebug() << "screens countss:" << listScreen.size();
    for (QScreen* screen : listScreen)
    {
        qDebug() << screen->name() << ": w-h:" << screen->geometry() << " depth:" << screen->depth() << " avibleGeometry:" << screen->availableGeometry();
//        if (screen == qApp->primaryScreen())
//            continue;

        SubWindow* newSubwin =  new SubWindow();
        screenMgr.setSubScreenWidget(newSubwin);
        a.connect(screen, &QScreen::geometryChanged, &screenMgr, &ScreenManager::showFullScreenAll);
    }

    a.connect(&a, &SingleApplication::instanceStarted, &screenMgr, &ScreenManager::showFullScreenAll);
    a.connect(&a, &QApplication::screenAdded, [&](QScreen* screen){
        qDebug() << "screen add:" << screen->name() << ": w-h:" << screen->geometry();
        SubWindow* newSub =  new SubWindow();
        screenMgr.setSubScreenWidget(newSub);
        a.connect(screen, &QScreen::geometryChanged, &screenMgr, &ScreenManager::showFullScreenAll);
        screenMgr.showFullScreenAll();
    });

    screenMgr.showFullScreenAll();
    w.setFocus();
    return a.exec();
}
